package com.yjc.hire.work.service.impl;

import com.yjc.eo.SearchResumeEO;
import com.yjc.hire.feign.UserMicroServiceFeign;
import com.yjc.hire.grace.result.GraceJSONResult;
import com.yjc.hire.grace.result.PageGridResult;
import com.yjc.hire.utils.JsonUtils;
import com.yjc.hire.utils.LocalDateUtils;
import com.yjc.hire.work.service.ResumeSearchService;
import com.yjc.hire.work.service.ResumeService;
import com.yjc.hire.work.vo.ResumeVO;
import com.yjc.hire.work.vo.UsersVO;
import org.checkerframework.checker.units.qual.A;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.IdsQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.IndexQuery;
import org.springframework.data.elasticsearch.core.query.IndexQueryBuilder;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.Query;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * </p>
 *
 * @author jcyin
 * @since 2025/9/8
 * </p>
 */

@Service
public class ResumeSearchServiceImpl implements ResumeSearchService {

    @Autowired
    private ResumeService resumeService;

    @Autowired
    private UserMicroServiceFeign userMicroServiceFeign;

    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    @Autowired
    private ElasticsearchRestTemplate template;
    @Override
    public void transformAndFlush(String userId) {

        SearchResumeEO resumeEO = new SearchResumeEO();
        resumeEO.setUserId(userId);
        //查询用户简历相关信息
        ResumeVO resumeVO = resumeService.getResumeInfo(userId);
        UsersVO userInfo = getHrUserInfo(userId);
        resumeEO.setResumeId(resumeVO.getId());
        resumeEO.setNickName(userInfo.getNickname());
        resumeEO.setSex(String.valueOf(userInfo.getSex()));
        resumeEO.setFace(String.valueOf(userInfo.getFace()));
        String formatted = LocalDateUtils.format(userInfo.getBirthday(), LocalDateUtils.DATE_PATTERN);
        long unitBetween = LocalDateUtils.getChronoUnitBetween(userInfo.getBirthday(),
                LocalDate.now(), ChronoUnit.YEARS, true);
        resumeEO.setBirthday(formatted);
        resumeEO.setAge((int) unitBetween);
        IndexQuery iq = new IndexQueryBuilder().withObject(resumeEO).build();
        template.index(iq, IndexCoordinates.of("resume_result"));


    }
    private UsersVO getHrUserInfo(String userId){
        GraceJSONResult result = userMicroServiceFeign.get(userId);
        Object data = result.getData();
        return JsonUtils.jsonToObject(data.toString(), UsersVO.class);
    }

    @Override
    public PageGridResult searchResumeByEs(SearchResumeEO bo,
                                           Integer page,
                                           Integer pageSize) {
        //todo();
        return null;
    }

    @Override
    public List<SearchResumeEO> searchResumeByEs(List<String> ids) {
        List<SearchResumeEO> resumeEOList = new ArrayList<>();
        if (ids == null) {
            return resumeEOList;
        }
        IdsQueryBuilder queryBuilder = new IdsQueryBuilder();
        for (String id : ids) {
            queryBuilder.addIds(id);
        }

        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        boolQueryBuilder.should(queryBuilder);
        Query query = new NativeSearchQueryBuilder().withQuery(boolQueryBuilder).build();
        SearchHits<SearchResumeEO> search = elasticsearchRestTemplate.search(query, SearchResumeEO.class);
        List<SearchHit<SearchResumeEO>> hits = search.getSearchHits();
        hits.forEach(hit -> {
            SearchResumeEO hitContent = hit.getContent();
            resumeEOList.add(hitContent);
        });
        return resumeEOList;
    }
}
